package com.shunbo.convenient.controller;

import com.github.pagehelper.Page;
import com.shunbo.convenient.resultVo.UserInfoVo;
import com.shunbo.convenient.service.*;
import com.shunbo.convenient.utils.DateUtils;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * 经营数据
 *
 * @author weizhuang
 */
@RestController
@RequestMapping("/data")
public class DataController {

    @Resource
    private OrdersService ordersService;
    @Resource
    private ProductOrderService productOrderService;
    @Resource
    private ProductService productService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private StoreService storeService;
    @Resource
    private BlessingService blessingService;


    /**
     * 用户数据
     *
     * @param order
     * @return
     */
    @RequestMapping("/data")
    public ConvenientResult getUserDate(@RequestBody Orders order) {

        Map<String, Object> resultMap = new HashMap<>(16);

        //营业额 毛利额
        Double money = 0.0;
        //退款金额
        Double tMoney = 0.0;
        //销售数量
        Integer num = 0;
        //客单价
        Double oddMoney = 0.0;
        //销售额
        Double price = 0.0;
        String createTime = order.getCreateTime();
        String[] str = createTime.split(" - ");
        List<Orders> all = ordersService.findBetween(str[0] + " 00:00:00", str[1] + " 23:59:59", order.getSid() + "");
        for (Orders orders : all
                ) {
            if (!orders.getStatus().equals("0") && !orders.getStatus().equals("5") && !orders.getStatus().equals("8")) {
                ProductOrder productOrder = new ProductOrder();
                Long id = orders.getId();
                productOrder.setOid(id + "");
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder po : all1
                        ) {
                    if (po.getPid() != null) {
                        Product byId = productService.findById(po.getPid() + "");
                        money += byId.getMoney() * po.getNum();
                        price += byId.getPrice() * po.getNum();
                        num += po.getNum();
                        if ("6".equals(orders.getStatus())) {
                            tMoney = byId.getMoney() * po.getNum();
                        }
                    }
                    if (po.getBid() != null) {
                        Blessing byId = blessingService.findById(po.getBid() + "");
                        money += byId.getMoney() * po.getNum();
                        price += byId.getMoney() * po.getNum();
                        num += po.getNum();
                        if ("6".equals(orders.getStatus())) {
                            tMoney = byId.getMoney() * po.getNum();
                        }
                    }
                }
            }
        }
        if (money != null && num != null && money != 0 && num != 0) {
            oddMoney = money / num;
        }
        Double rate = 0.0;
        if (price != null && price != 0.0) {
            rate = (price - money) / price * 100;
        }
        BigDecimal b = new BigDecimal(money);
        resultMap.put("money", b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        //TODO 毛利率 暂时不计算
        BigDecimal c = new BigDecimal(price - money);
        resultMap.put("mMoney", c.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        BigDecimal d = new BigDecimal(tMoney);
        resultMap.put("tMoney", d.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        resultMap.put("num", num);
        if (oddMoney != null) {
            System.out.println(oddMoney);
            BigDecimal e = new BigDecimal(oddMoney);
            resultMap.put("oddMoney", e.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        } else {

            resultMap.put("oddMoney", 0.0);
        }
        BigDecimal f = new BigDecimal(rate);
        resultMap.put("rate", f.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        return ConvenientResult.build(200, "成功", resultMap);
    }

    /**
     * 用户当天数据
     *
     * @param order
     * @return
     */
    @RequestMapping("/dataToDay")
    public ConvenientResult getUserDateToDay(@RequestBody Orders order) {

        Map<String, Object> resultMap = new HashMap<>(16);

        //营业额
        double money = 0.0;
        //销售数量
        Integer num = 0;
        //客单价
        double oddMoney = 0.0;
        order.setCreateTime(DateUtils.getDate());
        List<Orders> all = ordersService.findToDay(order.getSid() + "");
        for (Orders orders : all
                ) {
            ProductOrder productOrder = new ProductOrder();
            Long id = orders.getId();
            productOrder.setOid(id + "");
            List<ProductOrder> all1 = productOrderService.findAll(productOrder);
            for (ProductOrder po : all1
                    ) {
                if (po.getPid() != null) {
                    Product byId = productService.findById(po.getPid() + "");
                    money += byId.getMoney() * po.getNum();
                    num += po.getNum();
                }
                if (po.getBid() != null) {
                    Blessing byId = blessingService.findById(po.getBid() + "");
                    String[] productIds = byId.getProductIds().split(",");
                    Page<Product> byIds1 = productService.findByIds(Arrays.asList(productIds));
                    for (Product Product : byIds1.getResult()
                            ) {
                        money += Product.getMoney();
                    }
                    String profit = byId.getProfit().substring(0, byId.getProfit().length() - 1);
                    double v = money / 100 * (100 - Double.parseDouble(profit));
                    money += v * po.getNum();
                    num += po.getNum();
                }
            }
            oddMoney = money / all.size();
        }
        BigDecimal b = new BigDecimal(money);
        resultMap.put("money", b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        resultMap.put("num", num);
        BigDecimal c = new BigDecimal(oddMoney);
        resultMap.put("oddMoney", c.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue());
        return ConvenientResult.build(200, "成功", resultMap);
    }

    /**
     * 数据统计
     *
     * @param
     * @return
     */
    @RequestMapping("/dataList")
    public ConvenientResult getDataList(String startTime, String endTime) {


        List<UserInfo> userInfoList = userInfoService.findAll(null);

        List<UserInfoVo> userInfoVos = new ArrayList<>();
        for (UserInfo info : userInfoList
                ) {
            //营业额
            double money = 0.0;
            //销售数量
            Integer num = 0;
            //最高价格
            Double mixMoney = 0.0;
            Store store = new Store();
            store.setUser_id(info.getUserId());
            List<Store> all2 = storeService.findAll(store);
            Store byId1 = all2.get(0);
            UserInfoVo userInfoVo = new UserInfoVo();
            Orders orders = new Orders();
            orders.setUserId(info.getUserId());
            List<Orders> all = ordersService.findBetween(startTime, endTime, byId1.getId() + "");
            for (Orders order : all
                    ) {
                ProductOrder productOrder = new ProductOrder();
                Long id = order.getId();
                productOrder.setOid(id + "");
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder po : all1
                        ) {
                    if (po.getPid() != null) {
                        Double price = 0.0;
                        Product byId = productService.findById(po.getPid() + "");
                        money += byId.getMoney() * po.getNum();
                        price = byId.getMoney() * po.getNum();
                        num += po.getNum();
                        po.setProduct(byId);
                        if (price > mixMoney) {
                            mixMoney = price;
                        }
                    }
                    if (po.getBid() != null) {
                        Double price = 0.0;
                        Blessing byId = blessingService.findById(po.getBid() + "");
                        String[] productIds = byId.getProductIds().split(",");
                        Page<Product> byIds1 = productService.findByIds(Arrays.asList(productIds));
                        for (Product Product : byIds1.getResult()
                                ) {
                            money += Product.getMoney();
                        }
                        String profit = byId.getProfit().substring(0, byId.getProfit().length() - 1);
                        double v = money / 100 * (100 - Double.parseDouble(profit));
                        money += v * po.getNum();
                        num += po.getNum();
                        price = v * po.getNum();
                        po.setBlessing(byId);
                        if (price > mixMoney) {
                            mixMoney = price;
                        }
                    }
                }
                order.setProductOrder(all1);
            }
            userInfoVo.setOrdersList(all);
            userInfoVo.setNumber(num + "");
            userInfoVo.setMoney(money + "");
            userInfoVo.setMixMoney(mixMoney + "");
            userInfoVo.setStore(byId1);
            userInfoVos.add(userInfoVo);
        }
        return ConvenientResult.build(200, "成功", userInfoVos);
    }

}
